package com.zzyl.serve.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.serve.domain.NursingLevel;
import com.zzyl.serve.mapper.NursingLevelMapper;
import com.zzyl.serve.service.INursingLevelService;
import com.zzyl.serve.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
/**
 * 护理等级Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-27
 */
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper,NursingLevel>  implements INursingLevelService
{
    private static final String CACHE_KEY_PREFIX = "nursingLevel:all";

    @Autowired
    private NursingLevelMapper nursingLevelMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询护理等级
     * 
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id)
    {


        return this.getById(id);
    }

    /**
     * 查询护理等级列表
     * 
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevelVo> selectNursingLevelList(NursingLevel nursingLevel)
    {

        //分页查询护理等级
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel)
    {

        boolean flag = this.save(nursingLevel);
        //如果护理等级的列表会存储在Redis中，那么，对护理等级的所有增删改操作都需要清理Redis中的数据
        //删除缓存
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return flag? 1:0;
    }

    /**
     * 修改护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel)
    {

        boolean flag = this.updateById(nursingLevel);
        //删除缓存
        redisTemplate.delete(CACHE_KEY_PREFIX);

        return flag?1:0;
    }

    /**
     * 批量删除护理等级
     * 
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids)
    {

        boolean flag = this.removeByIds(Arrays.asList(ids));
        //删除缓存
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return flag ?1:0;

    }

    /**
     * 删除护理等级信息
     * 
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id)
    {
        boolean falg = this.removeById(id);
        //删除缓存
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return falg ?1:0;
    }

    /**
     * 查询护理等级列表
     *
     * @return
     */
    @Override
    public List<NursingLevel> getAll() {
        //先判断缓存中是否有数据
        List<NursingLevel>  list = (List<NursingLevel>) redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);

        //如果有数据，直接返回
        if (!CollectionUtils.isEmpty(list)){
            System.out.println("从缓存中获取数据");
            return list;
        }


        System.out.println("从数据库中获取数据");
        //如果没有数据，查询数据库，然后把数据返回
        list= this.list();
        redisTemplate.opsForValue().set(CACHE_KEY_PREFIX,list);
        return list;

    }

}
